home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5530 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.8 KB

  1. Path: dawn.mmm.com!news
  2. From: kjhopps@mmm.com (Kevin J Hopps)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: How to handle error in constructor
  5. Date: 5 Feb 1996 14:16:48 GMT
  6. Organization: 3M - St. Paul, MN  55144-1000 US
  7. Distribution: world
  8. Message-ID: <4f53gg$2oq@dawn.mmm.com>
  9. References: <4eo6n6$rbp@cnn.exu.ericsson.se> <1996Feb2.170821.1768@ittpub>
  10. Reply-To: kjhopps@mmm.com
  11. X-Newsreader: TIN [version 1.2 PL2]
  12.  
  13. Wil Evers (wil@ittpub.nl) wrote:
  14. WE> In article <4eo6n6$rbp@cnn.exu.ericsson.se> ebumow@ebu.ericsson.com  
  15. WE> (Mickey Williams 66753) writes:
  16. MW> In article 5EG@teslab.lab.oz.au,  andrew@teslab.lab.oz.au (Andrew  
  17. WE> Phillips) writes:
  18.  
  19. AP>I'm converting and enhancing a simple program in C to C++. Without
  20. AP>going into too much detail I have a class that represents a file on
  21. AP>disk. The contructor opens the file, but what should it do if the file
  22. AP>cannot be opened?  The C program just calls fopen() tests the return
  23. AP>value and if there's an error it prints a message and exits.
  24.  
  25. AP>In C++ I thought to set a flag in the constructor and have a member
  26. AP>function that tests the flag to see if the file was opened correctly.
  27.  
  28. MW> In practice, it is impossible to ensure that this works as intended.
  29. MW> Also, every user of this class must understand this error handling
  30. MW> mechanism. On the other hand, exceptions are part of the standard, so
  31. MW> throwing one of the standard exceptions should be much safer.
  32.  
  33. WE> There may be alternatives to throwing an exception here. Basically, disk  
  34. WE> file objects should behave reasonbly when the constructor couldn't open  
  35. WE> the requested file: for instance, instead of just assuming the fopen()  
  36. WE> succeeded, they could report failure when someone tries a read or write  
  37. WE> operation.   
  38.  
  39. AP>This seems rather inelegant -- I guess exceptions would be the elegant
  40. AP>way but seem like overkill for such a simple program.
  41.  
  42. MW> Elegant, schmelegant. It's the correct tool for the job.
  43. MW> [example deleted]
  44.  
  45. WE> Is it? Suppose, for instance, that I want to check for some parameter  
  46. WE> settings in an optional configuration file. Here, all I need to know is if  
  47. WE> there are any options to set. If there are none, I don't care why, so if  
  48. WE> the first read on the non-existing file simply returns EOF, I will be  
  49. WE> perfectly happy.
  50.  
  51. Whether a given function should be written to throw an exception or indicate
  52. failure in some other way depends largely on what would be convenient for its
  53. callers.  If, for most of them, failure of the function means failure for them,
  54. an exception may be best.  If, OTOH, failure of the function is of little
  55. consequence, a simple return code might be best.
  56.  
  57. Deciding how to report an error from a function whose users are unknown can
  58. be a problem.  One pretty much has to guess what the users will want.
  59.  
  60. It does not seem appropriate to fail silently.  This is what constructors
  61. and overloaded operators must do if they do not throw an exception on
  62. failure.
  63.  
  64. WE> If the disk file class further provides me with the option of telling me  
  65. WE> if the file could be opened - and, if not, why not, then I can decide for  
  66. WE> myself how sophisticated my error handling will be. 
  67.  
  68. WE> The problem with exceptions is, that I'm *required* to handle them or else  
  69. WE> my program will die - even if failure to open a file is nothing to be  
  70. WE> upset about. A decent disk file class should at least give me the option  
  71. WE> to say 'no thanks, no exceptions for me this time.'
  72.  
  73. Regardless of whether you're using exceptions or indicating errors in
  74. traditional ways like return codes, you can ignore errors or handle them.
  75. The differences are in what happens by default, and in when the errors are
  76. reported.  You can ignore return-codes very easily, but you can't use them
  77. from constructors and overloaded operators.  You can ignore exceptions too,
  78. but you have to write extra code to do it.
  79.  
  80. Personally, I like the idea of being *required* to handle errors, even if
  81. that means consciously throwing them away.
  82.  
  83. WE> IMHO exceptions are useful for handling major disasters, not for small  
  84. WE> inconveniences. Classes simply throwing an exception from their  
  85. WE> constructors because the class's programmer is too lazy to keep the  
  86. WE> object's state consistent in the face of such a failure are a nuisance at  
  87. WE> best, and could easily cause severe paranoia for the class's users.
  88.  
  89. A certain amount of paranoia with regard to error handling is a good thing,
  90. IMHO.  I think many programs would be much more robust if they were developed
  91. with a little more paranoia about errors.
  92. --
  93. Kevin J. Hopps                  e-mail: kjhopps@mmm.com
  94. 3M Company                      phone:  (612) 737-4643
  95. 3M Center, Bldg. 235-2D-57      fax:    (612) 737-2700
  96. St. Paul, MN 55144-1000         Opinions are my own.  I don't speak for 3M.
  97.     But 3M speaks for me -- I did not write the following line:
  98.  
  99. Opinions expressed herein are my own and may not represent those of 3M.
  100.